package com.tjhzzx.danfossNiagaraCore.servlet.impl;


//import BDashboardRoot;


import com.tjhzzx.danfossNiagaraCore.servlet.GetExecutor;
import com.tjhzzx.danfossNiagaraCore.servlet.PostExecutor;

import javax.baja.sys.Flags;
import javax.baja.sys.Property;
import javax.baja.sys.Sys;
import javax.baja.sys.Type;
import javax.baja.util.BFolder;
import javax.baja.web.BWebServlet;
import javax.baja.web.WebOp;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletResponse;
import java.util.logging.Level;
import java.util.logging.Logger;

//@NiagaraProperty(
//        name = "servletName", type = "baja:String",
//        defaultValue = "tlss"
//)

//@NiagaraType
public class BTlsWebServer extends BWebServlet {
    public static final Property servletName = newProperty(Flags.READONLY, "tlss", null);

    public String getServletName() {
        return getString(servletName);
    }

    public void setServletName(String v) {
        setString(servletName, v, null);
    }

    public static final Property title = newProperty(0, "The Tju System", null);

    public String getTitle() {
        return getString(title);
    }

    public void setTitle(String v) {
        setString(title, v, null);
    }

    public static final Property enTitle = newProperty(0, "The Tju System", null);

    public String getEnTitle() {
        return getString(enTitle);
    }

    public void setEnTitle(String v) {
        setString(enTitle, v, null);
    }

    public static final Property footer = newProperty(0, "2020 Intelligent Building Management System", null);

    public String getFooter() {
        return getString(footer);
    }

    public void setFooter(String v) {
        setString(footer, v, null);
    }

    public static final Property titleColor = newProperty(0, "#00f6ff", null);

    public String getTitleColor() {
        return getString(titleColor);
    }

    public void setTitleColor(String v) {
        setString(titleColor, v, null);
    }

    public static final Property subSystemsRoot = newProperty(0, new BFolder(), null);

    public BFolder getSubSystemsRoot() {
        return (BFolder) get(subSystemsRoot);
    }

    public void setSubSystemsRoot(BFolder v) {
        set(subSystemsRoot, v, null);
    }

    @Override
    public Type getType() {
        return TYPE;
    }

    public static final Type TYPE = Sys.loadType(BTlsWebServer.class);

    private static final Logger LOGGER = Logger.getLogger(BTlsWebServer.class.getName());

    private static final MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement("C:/temp");

    public static Logger getLogger() {
        return LOGGER;
    }

//    protected String COMMAND_LOCAL_PKG = "hnceCommand";

    @Override
    public void doPost(WebOp op) throws Exception {
        // Get request path
        String path = op.getRequest().getPathInfo().substring(1);
        try {
//            getLicenseInfo();
            PostExecutor servletCommand = getPostCommand(path);
            servletCommand.doPost(op);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "doPost exception", e);
            op.getResponse().getWriter().write("{\"message\":\"" + e.getMessage() + "\" }");
            op.getResponse().setStatus(HttpServletResponse.SC_OK);
        }
    }

    @Override
    public void doGet(WebOp op) throws Exception {
        // Get request path
        String path = op.getRequest().getPathInfo().substring(1);
        try {
//            getLicenseInfo();
            GetExecutor servletCommand = getGetCommand(path);
            servletCommand.doGet(op);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "doGet exception", e);
            op.getResponse().getWriter().write("{message:\"" + e.getMessage() + "\" }");
            op.getResponse().setStatus(HttpServletResponse.SC_OK);
        }
    }

    private PostExecutor getPostCommand(String path) throws ClassNotFoundException, IllegalAccessException,
            InstantiationException {
        String[] moduleClass = path.split("/");
        if(null == moduleClass || moduleClass.length != 2) {
            throw new ClassNotFoundException();
        }
        String className = "com.tjhzzx." + moduleClass[0] + ".command." + moduleClass[1] + "Command";
        Class c = Sys.loadClass(moduleClass[0], className);
        return (PostExecutor) c.newInstance();
    }

    private GetExecutor getGetCommand(String path) throws ClassNotFoundException, IllegalAccessException,
            InstantiationException {
        String[] moduleClass = path.split("/");
        if(null == moduleClass || moduleClass.length != 2) {
            throw new ClassNotFoundException();
        }
        String className = "com.tjhzzx." + moduleClass[0] + ".command." + moduleClass[1] + "Command";
        Class c = Sys.loadClass(moduleClass[0], className);
        return (GetExecutor) c.newInstance();
    }


}
